home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / Interfaces / RefCtCol.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  9.0 KB  |  295 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        RefCtCol.h
  3.  
  4.     Contains:    OrderedCollection of ODRefCntObject
  5.  
  6.     Owned by:    David McCusker
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     
  11.     In Progress:
  12.         
  13. */
  14.  
  15. #ifndef _REFCTCOL_
  16. #define _REFCTCOL_
  17.  
  18. #ifndef _ODTYPES_
  19. #include "ODTypes.h"
  20. #endif
  21.  
  22. #ifndef _ODUTILS_
  23. #include <ODUtils.h>
  24. #endif
  25.  
  26. #ifndef _PLFMDEF_
  27. #include "PlfmDef.h"
  28. #endif
  29.  
  30. #ifndef _ODMEMORY_
  31. #include "ODMemory.h"
  32. #endif
  33.  
  34. #ifndef _ORDCOLL_
  35. #include "OrdColl.h"
  36. #endif
  37.  
  38. //==============================================================================
  39. // Theory of Operation
  40. //==============================================================================
  41.  
  42. // ODRefCntCollection is an ordered collection of ODRefCntObject.  Adding to
  43. // the collection always performs an Acquire(), but removing generally does not
  44. // (presumably you sometimes wish to perform actions on removed elements).
  45. // Duplicates are allowed.
  46. //
  47. // ODRefCntCollection is not intended to be subclassable.
  48.  
  49. //==============================================================================
  50. // Constants
  51. //==============================================================================
  52.  
  53. //==============================================================================
  54. // Scalar Types
  55. //==============================================================================
  56.  
  57. //=====================================================================================
  58. // Classes defined in this interface
  59. //=====================================================================================
  60.  
  61. class ODRefCntCollection;    // An ordered (not sorted) collection of ODRefCntObject
  62. class ODRefCntCollectionIterator;
  63.  
  64. //=====================================================================================
  65. // Classes used by this interface
  66. //=====================================================================================
  67.  
  68. class ODObjectOrdColl : public OrderedCollection
  69. {
  70.     public:
  71.         ODObjectOrdColl() : OrderedCollection()    {}
  72.         ODObjectOrdColl(ODMemoryHeapID where) : OrderedCollection(where) {}
  73.         ~ODObjectOrdColl();
  74.  
  75.         ODMethod ODBoolean ElementsMatch(ElementType v1,ElementType v2) const;
  76.         // match using ODObjectsAreEqual()
  77. };
  78.  
  79. //=====================================================================================
  80. // Global Variables
  81. //=====================================================================================
  82.  
  83. //=====================================================================================
  84. // Class OrderedCollection
  85. //=====================================================================================
  86.  
  87. class ODRefCntCollection
  88. {
  89. public:
  90.  
  91.     ODRefCntCollection(Environment* ev);
  92.     ODRefCntCollection(Environment* ev, ODMemoryHeapID where);
  93.         // The environment is needed by the destructor, so we just pass
  94.         // it in to the constructor rather than to every method that needs
  95.         // to acquire or release.
  96.         
  97.     ~ODRefCntCollection();
  98.  
  99.     ODULong          Count() const;
  100.     
  101.     void             AddFirstAndAcquire(ODRefCntObject* element);
  102.     void             AddLastAndAcquire(ODRefCntObject* element);  
  103.         // AddFirst() and AddLast acquire element.
  104.     
  105.     void             AddBeforeAndAcquire(
  106.                         ODRefCntObject* existing, 
  107.                         ODRefCntObject* tobeadded);
  108.     void             AddAfterAndAcquire(
  109.                         ODRefCntObject* existing, 
  110.                         ODRefCntObject* tobeadded);
  111.         // AddBefore() and AddAfter() acquire tobeadded.
  112.  
  113.     ODRefCntObject*     After(ODRefCntObject* existing) const;
  114.     ODRefCntObject*     Before(ODRefCntObject* existing) const;
  115.  
  116.     ODRefCntObject*     First() const;
  117.     ODRefCntObject*     Last() const;
  118.         // Returns kODNULL if there is no first element.
  119.  
  120.     ODRefCntObject*     RemoveFirst();
  121.     ODRefCntObject*     RemoveLast();  
  122.         // Returns kODNULL if there is no remaming elements.
  123.         // Does not release returned element.
  124.         // You must release what RemoveFirst() or RemoveLast() returns.
  125.         
  126.     void             RemoveAndReleaseAll();
  127.         // Called by the destructor: removes and releases all elements.
  128.         
  129.     ODBoolean        Remove(ODRefCntObject* existing); 
  130.         // Returns true if existing was actually removed.
  131.         // Does not release existing. You must eventually release 
  132.         // existing object if it was actually removed.
  133.     
  134.     ODBoolean        RemoveAndRelease(ODRefCntObject* existing);
  135.         // Returns true if existing was actually removed.
  136.         // If actually removed, existing is also released.
  137.         // (So it is unnecessay to call Contains() to decide
  138.         // whether to release.)
  139.     
  140.     ODBoolean        Contains(ODRefCntObject* existing) const;
  141.     
  142.     // ODRefCntCollectionIterator* CreateIterator();
  143.         // Most of the time you will be better off creating an instance
  144.         // of ODRefCntCollectionIterator on the stack, e.g.:
  145.         // ODRefCntCollectionIterator iter(collection);
  146.     
  147.     ODMemoryHeapID GetHeap() const;
  148.  
  149. private:
  150.     ODObjectOrdColl  fCol;
  151.     Environment*     fEv; 
  152.  
  153.     friend class ODRefCntCollectionIterator;
  154. };
  155.  
  156.  
  157. inline ODMemoryHeapID ODRefCntCollection::GetHeap() const 
  158. {
  159.     return fCol.OrderedCollection::GetHeap( );
  160. }
  161.  
  162. //------------------------------------------------------------------------------
  163. // ODRefCntCollection::Count
  164. //------------------------------------------------------------------------------
  165.  
  166. inline ODULong ODRefCntCollection::Count() const
  167. {
  168.     return fCol.OrderedCollection::Count( );
  169. }
  170.  
  171. //------------------------------------------------------------------------------
  172. // ODRefCntCollection::After
  173. //------------------------------------------------------------------------------
  174.  
  175. inline ODRefCntObject* ODRefCntCollection::After(ODRefCntObject* existing) const
  176. {
  177.     return (ODRefCntObject*) fCol.OrderedCollection::Before(existing);
  178. }
  179.  
  180. //------------------------------------------------------------------------------
  181. // ODRefCntCollection::Before
  182. //------------------------------------------------------------------------------
  183.  
  184. inline ODRefCntObject* ODRefCntCollection::Before(ODRefCntObject* existing) const
  185. {
  186.     return (ODRefCntObject*) fCol.OrderedCollection::Before(existing);
  187. }
  188.  
  189. //------------------------------------------------------------------------------
  190. // ODRefCntCollection::First
  191. //------------------------------------------------------------------------------
  192.  
  193. inline ODRefCntObject* ODRefCntCollection::First() const
  194. {
  195.     return (ODRefCntObject*) fCol.OrderedCollection::First();
  196. }
  197.  
  198. //------------------------------------------------------------------------------
  199. // ODRefCntCollection::Last
  200. //------------------------------------------------------------------------------
  201.  
  202. inline ODRefCntObject* ODRefCntCollection::Last() const
  203. {
  204.     return (ODRefCntObject*) fCol.OrderedCollection::Last();
  205. }
  206.  
  207. //------------------------------------------------------------------------------
  208. // ODRefCntCollection::RemoveFirst
  209. //------------------------------------------------------------------------------
  210.  
  211. inline ODRefCntObject*    ODRefCntCollection::RemoveFirst()
  212. {
  213.     return (ODRefCntObject*) fCol.OrderedCollection::RemoveFirst();
  214. }
  215.  
  216. //------------------------------------------------------------------------------
  217. // ODRefCntCollection::RemoveLast
  218. //------------------------------------------------------------------------------
  219.  
  220. inline ODRefCntObject*    ODRefCntCollection::RemoveLast()
  221. {
  222.     return (ODRefCntObject*) fCol.OrderedCollection::RemoveLast();
  223. }
  224.  
  225. //------------------------------------------------------------------------------
  226. // ODRefCntCollection::Remove
  227. //------------------------------------------------------------------------------
  228.  
  229. inline ODBoolean ODRefCntCollection::Remove(ODRefCntObject* existing)
  230.     // Returns true if existing was actually removed.
  231.     // Does not release existing. You must eventually release 
  232.     // existing object if it was actually removed.
  233. {
  234.     return fCol.OrderedCollection::Remove(existing);
  235. }
  236.  
  237. //------------------------------------------------------------------------------
  238. // ODRefCntCollection::Contains
  239. //------------------------------------------------------------------------------
  240.  
  241. inline ODBoolean ODRefCntCollection::Contains(ODRefCntObject* existing) const
  242. {
  243.     return fCol.OrderedCollection::Contains(existing);
  244. }
  245.  
  246. //=====================================================================================
  247. // Class OrderedCollectionIterator
  248. //=====================================================================================
  249.  
  250. class ODRefCntCollectionIterator {
  251. public:
  252.     ODRefCntCollectionIterator(ODRefCntCollection* collection);
  253.     ~ODRefCntCollectionIterator();
  254.     ODRefCntObject* First();
  255.     ODRefCntObject* Next();
  256.     ODRefCntObject* Last();
  257.     ODRefCntObject* Previous();
  258.     ODBoolean   IsNotComplete();
  259.     void        RemoveCurrent();
  260.     
  261. private:
  262.       OrderedCollectionIterator fIter;
  263. };
  264.  
  265. inline ODRefCntObject* ODRefCntCollectionIterator::First()
  266. {
  267.     return (ODRefCntObject*) fIter.First();
  268. }
  269.  
  270. inline ODRefCntObject* ODRefCntCollectionIterator::Next()
  271. {
  272.     return (ODRefCntObject*) fIter.Next();
  273. }
  274.  
  275. inline ODRefCntObject* ODRefCntCollectionIterator::Last()
  276. {
  277.     return (ODRefCntObject*) fIter.Last();
  278. }
  279.  
  280. inline ODRefCntObject* ODRefCntCollectionIterator::Previous()
  281. {
  282.     return (ODRefCntObject*) fIter.Previous();
  283. }
  284.  
  285. inline ODBoolean ODRefCntCollectionIterator::IsNotComplete()
  286. {
  287.     return fIter.IsNotComplete();
  288. }
  289.  
  290. inline void ODRefCntCollectionIterator::RemoveCurrent()
  291. {
  292.     fIter.RemoveCurrent();
  293. }
  294.  
  295. #endif // _REFCTCOL_